<?php 
class Am_Form_CreditCard_Moip extends Am_Form_CreditCard
{
    public function init()
    {
        $name = $this->addGroup()->setLabel(array(___('Cardholder Name'), sprintf(___('cardholder first and last name, exactly as%son the card'), '<br/>')));
        $name->addRule('required', ___('Please enter credit card holder name'));
        $name_f = $name->addText('cc_name_f', array('size'=>15));
        $name_f->addRule('required', ___('Please enter credit card holder first name'))->addRule('regex', ___('Please enter credit card holder first name'), '|^[a-zA-Z_\' -]+$|');
        $name_l = $name->addText('cc_name_l', array('size'=>15));
        $name_l->addRule('required', ___('Please enter credit card holder last name'))->addRule('regex', ___('Please enter credit card holder last name'), '|^[a-zA-Z_\' -]+$|');

        $options = $this->plugin->getFormOptions();

        if (in_array(Am_Paysystem_CreditCard::CC_COMPANY, $options))
            $company = $this->addText('cc_company')->setLabel(array(___('Company Name'), sprintf(___('the company name associated with the%sbilling address for the transaction'), '<br/>')));
        
        if (in_array(Am_Paysystem_CreditCard::CC_TYPE_OPTIONS, $options))
        {
            $type = $this->addSelect('cc_type')->setLabel(array(___('Credit Card Type'), ___('please select one')))
                ->loadOptions(array_merge(array(''=>'-- ' . ___('Please choose') . ' --'), 
                    $this->plugin->getCreditCardTypeOptions()));
            $type->addRule('required', ___('Please choose a Credit Card Type'));
        }

        if ($this->formType == self::ADMIN_UPDATE)
        {
            $group = $this->addGroup()->setLabel(___('Credit Card Number'), ___('for example: 1111-2222-3333-4444'));
            $group->addStatic('cc');
            $cc = $group->addText('cc_number', array('autocomplete'=>'off', 'size'=>22, 'maxlength'=>22, 'style'=>'display:none'));
            $cc->addRule('regex', ___('Invalid Credit Card Number'), '/^[0-9 -]+$/')
                ->addRule('callback2', 'Invalid CC#', array($this->plugin, 'validateCreditCardNumber'));
            $group->addScript("")->setScript(<<<CUT
$(function(){
    $("input#cc_number-0").closest(".element").click(function(){
        var input = $("input#cc_number-0").detach();
        $(this).empty().append(input.show());
    });
});
CUT
);
        } else {
            $cc = $this->addText('cc_number', array('autocomplete'=>'off', 'size'=>22, 'maxlength'=>22))
                    ->setLabel(___('Credit Card Number'), ___('for example: 1111-2222-3333-4444'));
            $cc->addRule('required', ___('Please enter Credit Card Number'))
                ->addRule('regex', ___('Invalid Credit Card Number'), '/^[0-9 -]+$/')
                ->addRule('callback2', 'Invalid CC#', array($this->plugin, 'validateCreditCardNumber'));
        }

        $expire = $this->addElement(new Am_Form_Element_CreditCardExpire('cc_expire'))
            ->setLabel(array(___('Card Expire'), ___('Select card expiration date - month and year')));

        
        if (in_array(Am_Paysystem_CreditCard::CC_CODE, $options))
        {
            $code = $this->addPassword('cc_code', array('autocomplete'=>'off', 'size'=>4, 'maxlength'=>4))
                    ->setLabel(___('Credit Card Code'), sprintf(___('The "Card Code" is a three- or four-digit security code%sthat is printed on the back of credit cards in the card\'s%ssignature panel (or on the front for American Express cards).'),'<br>','<br>'));
            $code->addRule('required', ___('Please enter Credit Card Code'))
                 ->addRule('regex', ___('Please enter Credit Card Code'), '/^\s*\d{3,4}\s*$/');
        }
        if (in_array(Am_Paysystem_CreditCard::CC_MAESTRO_SOLO_SWITCH, $options))
        {
            $issue = $this->addText('cc_issuenum', array('autocomplete'=>'off', 'size'=>20, 'maxlength'=>22))
                    ->setLabel(___('Card Issue #'), ___('is required for Maestro/Solo/Switch credit cards only'))
                    ->addRule('regex', ___('Invalid Issue Number'), '/^\d+$/');
            $this->addElement(new Am_Form_Element_CreditCardExpire('cc_startdate', null, array('dont_require'=>true, 'years'=>-10)))
                ->setLabel(array(___('Card Start Date'), ___('is required for Maestro/Solo/Switch credit cards only')));
        }
        if (in_array(Am_Paysystem_CreditCard::CC_INPUT_BIN, $options))
        {
            $fieldSet = $this->addFieldset()->setLabel(___('Bank Identification'));
            $fieldSet->addText('_cc_bin_name', array())
               ->setLabel(array(___('Bank Name'), ___('name of the bank which issued the credit card')));
            $fieldSet->addText('_cc_bin_phone', array())
               ->setLabel(array(___('Bank Phone'), ___('customer service phone number listed on back of your credit card')));
        }
        
        $fieldSet = $this->addFieldset(___('Address Info'))->setLabel(array(___('Address Info'), ___('(must match your credit card statement delivery address)')));
        $street = $fieldSet->addText('cc_street')->setLabel(___('Street Address'))
                           ->addRule('required', ___('Please enter Street Address'));
        if (in_array(Am_Paysystem_CreditCard::CC_STREET2, $options))
        {
            $street2 = $fieldSet->addText('cc_street2')->setLabel(___('Street Address (Second Line)'))
                           ->addRule('required', ___('Please enter Street Address'));
        }
        if (in_array(Am_Paysystem_CreditCard::CC_HOUSENUMBER, $options))
        {
            $house = $fieldSet->addText('cc_housenumber', array('size'=>15))->setLabel(___('Housenumber'))
                    ->addRule('required', ___('Please enter housenumber'));
        }
        $city = $fieldSet->addText('cc_city')->setLabel(___('City'))
                         ->addRule('required', ___('Please enter City'));
        if (in_array(Am_Paysystem_CreditCard::CC_PROVINCE_OUTSIDE_OF_US, $options))
        {
            $province = $fieldSet->addText('cc_province', array('size'=>15))->setLabel(array(___('Billing International Province'), ___('for international provinces outside of US & Canada include the province name here')))
                    ->addRule('required', ___('Please choose state'));
        }

        $zip = $fieldSet->addText('cc_zip')->setLabel(___('ZIP'))
                        ->addRule('required', ___('Please enter ZIP code'));
        $country = $fieldSet->addSelect('cc_country')->setLabel(___('Country'))
            ->setId('f_cc_country')
            ->loadOptions(Am_Di::getInstance()->countryTable->getOptions(true));
        $country->addRule('required', ___('Please enter Country'));

        $group = $fieldSet->addGroup()->setLabel(___('State'));
        $group->addRule('required', ___('Please enter State'));
        /** @todo load correct states */
        $stateSelect = $group->addSelect('cc_state')
            ->setId('f_cc_state')
            ->loadOptions($stateOptions = Am_Di::getInstance()->stateTable->getOptions(@$_REQUEST['cc_country'], true));
        $stateText = $group->addText('cc_state')->setId('t_cc_state');
        $disableObj = $stateOptions ? $stateText : $stateSelect;
        $disableObj->setAttribute('disabled', 'disabled')->setAttribute('style', 'display: none');

        if (in_array(Am_Paysystem_CreditCard::CC_PHONE, $options))
        {
            $phone = $fieldSet->addText('cc_phone', array('size'=>14))->setLabel(___('Phone'))
                    ->addRule('required', ___('Please enter phone number'))
                    ->addRule('regex', ___('Please enter phone number'), '|^[\d() +-]+$|');
        }       
   
         $buttons = $this->addGroup();         
         $buttons->addStatic('')->setContent(<<<CUT
             <script type="text/javascript">
                 var funcaoSucesso = function(data)
                 {
                     console.log(data);
                     window.open(data.url);
                 };
 
                 var funcaoFalha = function(data) 
                 {
                     console.log(data);
                 };            

                 processPaymentCreditCard = function()
                 {                     
                     var settings = {
                	    "Forma": "",
                	    "Instituicao": "",
                	    "Parcelas": "",
                	    "Recebimento": "",
                	    "CartaoCredito": {
                    	    "Numero": "",
                    	    "Expiracao": "",
                    	    "CodigoSeguranca": "",
                    	    "Portador": {
                    	        "Nome": "",
                    	        "DataNascimento": "",
                    	        "Telefone": "",
                    	        "Identidade": ""                    	        
                    	    }
                	    }
                     }
                 
                     MoipWidget(settings);  
                 }                     
            
             </script>
CUT
 );         
         $buttons->addStatic("")->setContent(<<<CUT
             <div id="MoipWidget"
                 data-token=""
                 callback-method-success="funcaoSucesso"
                 callback-method-error="funcaoFalha"></div>
CUT
);
         $buttons->addSubmit('_cc_', array('value'=>
                 '    '
                 . $this->payButtons[ $this->formType ]
                 .'    ',
                 'onclick' => 'processPaymentCreditCard()'));
         $buttons->addStatic("")->setContent(<<<CUT
             <script type='text/javascript' src='https://desenvolvedor.moip.com.br/sandbox/transparente/MoipWidget-v2.js' 
                 charset=ISO-8859-1"></script>  
CUT
);
         
         $this->plugin->onFormInit($this);
    }
}